package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //使用sdiff方法，计算redis中两个集合的差值
        Set<String> imgs = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (imgs.size()>0 && imgs !=null){
            for (String img : imgs) {
                //从七牛云中删除多余图片
                QiniuUtils.deleteFileFromQiniu(img);
                //从redis中删除多余图片
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,img);
                System.out.println("清理"+img);
            }
        }
    }
}
